package com.rytong.tools.utils;

import com.rytong.tools.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LPToastUtil {

	public static LPToastUtil instance;

	/**
	 * 判断是否进入分行流程
	 */
	public boolean isBranchBank = false; 
	/**
	 * 判断是否是含有Style和javascript的方法，实际判断是否是二代规范报文
	 */
	public boolean isHasStyle = false;
	public boolean isNotPushStack=false;
	/**
	 * 是否已经建立加密信道，false-未建立，true-建立完成
	 */
	public static boolean isBuildChannel = false;
	/**
	 * 是否已经下载完成离线资源，false-未更新，true-更新完成
	 */
	public static boolean isUpdateOffline = false;
	public LPToastUtil() {

	}

	public static LPToastUtil getInstance() {
		if (instance == null) {
			instance = new LPToastUtil();
		}
		return instance;
	}

	public void getToast(String errorInfo, final Context act) {
		final Dialog alert = createToast(errorInfo, act);
		alert.show();
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(!((Activity)act).isFinishing())
					alert.cancel();
			}
		});
		t.start();
	}

	/**
	 * 创建toast弹出框
	 * 
	 * **/
	protected Dialog createToast(String errorInfo, Context act) {
		RelativeLayout line = (RelativeLayout) LayoutInflater.from(act)
				.inflate(R.layout.iccard_toast, null);
		TextView tv = (TextView) line.findViewById(R.id.tv);
		tv.setText(errorInfo);

		Dialog alert = new Dialog(act, R.style.dialog);// 在values下创建一个styles.xml文件，代码见后
		alert.setContentView(line);
		Window mWindow = alert.getWindow();
		mWindow
				.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		WindowManager.LayoutParams lp = mWindow.getAttributes();

		lp.dimAmount = 0f; // 弹出对话框后去除变暗阴影
		// lp.alpha = 1.0f;
		return alert;
	}

	/**
	 * 自定义对话框 ，提示消息
	 * @param activity
	 * @param message
	 */
	public static void showAlertMessage(Activity activity ,String message){
		final Dialog dialog = new Dialog(activity,R.style.dialog);
		dialog.show();
		
		Window window = dialog.getWindow();
		int resourceId = Utils.getResourcesId(activity, "layout_alert_define", "layout");
		window.setContentView(resourceId);
		window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		//弹出对话框后去除变暗阴影
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = 0f;
		
		//设置视图的宽高
		int mainId = Utils.getResourcesId(activity, "alert_main", "id");
		RelativeLayout main = (RelativeLayout)window.findViewById(mainId);
		main.setLayoutParams(new RelativeLayout.LayoutParams(Utils.getScaledValueX(260), Utils.getScaledValueY(160)));
		
		//设置显示内容
		int contentId = Utils.getResourcesId(activity, "alert_content", "id");
		TextView content = (TextView)window.findViewById(contentId);
		content.setText(message);
		
		//设置按钮高度和点击事件
		int confirmId = Utils.getResourcesId(activity, "alert_confirm", "id");
		Button confirm = (Button)window.findViewById(confirmId);
		RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) confirm.getLayoutParams();
		rl.height = Utils.getScaledValueY(32);
		
		confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
	}
}
